﻿using System.Security.Permissions;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using Com.FastSearch.SharePoint.Interfaces;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    /// <summary>
    /// A web part that presents the user's query and any navigation/drill-down applied.
    /// Allows drill-down to be removed by clicking on the applied modifier items that have
    /// a remove-icon associated with them.
    /// </summary>
    public class ESPBreadcrumb : ESPSearchWebPart
    {
        #region Private variables

        private const string BREADCRUMBS_ELEMENT = "Breadcrumbs";
        private const string QUERY_ELEMENT = "Query";
        private const string DRILL_DOWN_ELEMENT = "DrillDown";
        private const string FIELD_ELEMENT = "Field";
        private const string VALUE_ELEMENT = "Value";
        private const string UNIT_ELEMENT = "Unit";
        private const string REMOVE_HREF_ELEMENT = "RemoveHref";

        private IESPSearchResult _searchResult;

        #endregion

        #region DataFormWebPart members

        /// <summary>
        /// Get a handle to the xml document representing the query and applied navigation,
        /// to be rendered using XSLT.
        /// </summary>
        /// <param name="viewPath">not used</param>
        /// <returns>An XPathNavigator for the xml document</returns>
        [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            XmlDocument doc = new XmlDocument();
            if (_searchResult != null)
            {
                XmlNode root = doc.AppendChild(doc.CreateElement(BREADCRUMBS_ELEMENT));
                XmlNode query = root.AppendChild(doc.CreateElement(QUERY_ELEMENT));
                query.InnerText = SearchRequest.UserQuery;
                foreach (IESPNavigator navigator in _searchResult.Navigators)
                {
                    IESPNavigatorDefinition def = GetNavigator(navigator.Name);
                    foreach (IESPNavigatorItem item in navigator.AppliedNavigatorItems)
                    {
                        XmlNode drillDown = root.AppendChild(doc.CreateElement(DRILL_DOWN_ELEMENT));

                        XmlNode field = drillDown.AppendChild(doc.CreateElement(FIELD_ELEMENT));
                        field.InnerText = navigator.DisplayName;

                        XmlNode value = drillDown.AppendChild(doc.CreateElement(VALUE_ELEMENT));
                        value.InnerText = item.DisplayValue;

                        XmlNode unit = drillDown.AppendChild(doc.CreateElement(UNIT_ELEMENT));
                        unit.InnerText = navigator.Unit;

                        XmlNode removeHref = drillDown.AppendChild(doc.CreateElement(REMOVE_HREF_ELEMENT));
                        NavigationHelperParameter navParameter = new NavigationHelperParameter(Page.Request);
                        NavigationHelper.RemoveParameter(navParameter, def != null && def.IsScopeNavigator ? "scopenav" : "nav", item.GetNavigationParam(def != null ? def.Scope : null, SearchRequest.NavSplitCharacter), ESPSearchGroupID);
                        NavigationHelper.RemoveParameter(navParameter, "start1", ESPSearchGroupID);
                        removeHref.InnerText = navParameter.RawUrl;
                    }
                }
            }
            return doc.CreateNavigator();
        }

        #endregion

        #region ESPSearchWebPart members

        /// <summary>
        /// Set any parameters on the search required by this web-part.
        /// </summary>
        protected override void ConfigureSearch()
        {
        }

        /// <summary>
        /// Set the search-result. Called when the search has been performed.
        /// </summary>
        /// <param name="SearchResult">The search result</param>
        public override void SetSearchResult(IESPSearchResult SearchResult)
        {
            _searchResult = SearchResult;
        }

        /// <summary>
        /// Get the XSL stylesheet bundled with the web-part.
        /// </summary>
        /// <returns>The default XSL to be used when rendering this web-part</returns>
        public override string GetEmbeddedXsl()
        {
            return LoadXslFromAssembly("Breadcrumbs.xslt");
        }

        #endregion

        #region Private helpers

        private IESPNavigatorDefinition GetNavigator(string navigator)
        {
            return GetSearchManager().GetNavigatorsForView(SearchRequest.ESPSearchView)[navigator];
        }

        #endregion
    }
}
